<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>首页</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <script src="__PUBLIC__/js/jquery-1.9.0.js"></script>
    <script src="__PUBLIC__/home/plugins/layer/layer.js"></script>
    <script src="__PUBLIC__/js/angular.js"></script>
    <!--标准mui.css-->
    <link rel="stylesheet" href="__PUBLIC__/mui/css/mui.css" />
    <!--App自定义的css-->
    <link rel="stylesheet" href="__PUBLIC__/css/app.css" />
    <link rel="stylesheet" href="__PUBLIC__/css/cart.css" />
    <script src="__PUBLIC__/mui/js/mui.min.js"></script>
    <!--footer-->
    <script src="__PUBLIC__/js/cart.js"></script>
    <script src="__PUBLIC__/common.js"></script>
    <style>
        .tip{
            height: 35px;background: #ffefd4;
            color: #f96230;
            line-height: 35px;
            padding-left: 1.5em;
            font-size: 16px;

        }
        .agentmsg .time{
            width: 50%;
            height: 80px;
        }
        .agentmsg{
            height: 120px;
            text-align: center;
            box-sizing: border-box;
            padding-top: 10px;
        }
        .agent-title{
            font-size: 18px;
            color: #666666;
        }
        .agent-content{
            color: #212121;
            font-weight: bold;
            font-size: 24px;
        }
        .s_center {
            width: auto;
            padding: 0 50%;
            margin: 0;
        }
        .s_center option{
            text-align: center;
            font-weight: bold;
            font-size: 24px;
        }
        .good-name{color: black;font-size: 16px;font-weight: bold;font-family: "Microsoft YaHei UI Light"}
        .good-name b{margin-left: 2em;color: #999999;font-family: "Microsoft YaHei UI Light";}
        .good-price{color: #f2373b;font-size: 16px}
        .good-total{color: black;font-size: 16px}
        .good-total b{color: #f2373b}
        #money{  color: #f2373b;  }
        .cart-total {bottom: 0;}
    </style>
</head>
<body ng-app="myApp" ng-controller="Ctrl" ng-cloak style="height: 100%;" >
<!--header-->
<header class="mui-bar mui-bar-nav" >
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: white"></a>
    <h1 class="mui-title">代理升级</h1>
</header>
<div class="mui-input-group" style="padding-top: 44px">
    <div class="mui-row tip">
        提示:最低拿货金额{{limitmoney}}元
    </div>
    <div class="mui-input-row agentmsg" style="background:white;height: 100px">
        <label class="time"> <p class="agent-title">代理级别</p>
            <p>
                <select class="s_center" ng-model="selectlevel" ng-change="changelevel()">
                    <option ng-repeat="x in agenlevel" value="{{x.level}}" ng-selected="x.level == level">{{x.name}}</option>
                </select>
            </p>
        </label>
        <label class="time"> <p class="agent-title">金额(￥)</p>
            <p class="agent-content"> {{limitmoney}}</p>
        </label>
    </div>
</div>
<!--购物车-->
<div class="mui-content cart" style="padding-top: 0">
    <ul class="mui-table-view" style="margin-top: 3px">
        <li class="mui-table-view-cell mui-media cart-goods" ng-repeat="pro in cart"> <a href="javascript:;">
            <div class="mui-media-body">
                <div class="mui-numbox mui-pull-right" data-numbox-min="0">
                    <button class="mui-btn mui-btn-numbox-minus" data-numbox-min="0" type="button" ng-click="pro.num = pro.num -1;jisuan(pro.id)">- </button>
                    <input class="mui-input-numbox" type="number" ng-model="pro.num" ng-change="jisuan(pro.id)" />
                    <button class="mui-btn mui-btn-numbox-plus" type="button" ng-click="pro.num = pro.num + 1;jisuan(pro.id)">+ </button>
                </div>
                <p class="mui-ellipsis good-name">{{pro.name}}
                    <b><span ng-hide="pro.num*1>=pro.boxnum*1">{{pro.num}}盒</span>
                        <span ng-show="pro.num*1>=pro.boxnum*1">{{floor(pro.num/pro.boxnum)}}箱{{pro.num%pro.boxnum}}盒</span>
                    </b>
                </p>
                <p class="mui-ellipsis mui-pull-left good-price">￥{{pro.myprice}}</p>
                <p class="mui-ellipsis mui-pull-right good-total">小计:<b>￥{{pro.prozong.toFixed(2)}}</b></p>
            </div> </a> </li>
    </ul>
</div>
<div style="height: 100px">
</div>
<!--结算&删除-->
<div class="cart-total "  style="position: fixed;bottom: 50px;width: 100%">
    <ul class="mui-table-view">
        <li class=" mui-media ">
            <div class="mui-media-body  do-cart" style="display: flex">
                <p class="money-all" style="padding-left: 4em">合计:<span id="money">￥{{zong.toFixed(2)}}</span></p>
                <p class="go-deal " id="go-deal" style=" overflow:visible" ng-click="sub()">确认</p>
            </div>
        </li>
    </ul>
</div>

<include file="./Application/Home/View/pub/agentfoot.html" name="m"/>
<script>
    var app = angular.module("myApp", []);

    app.controller('Ctrl', function ($scope, $http) {
        $http({
            method: 'GET',
            url: "/index.php/Home/Agen/getAgenUpInfo"
        }).then(function successCallback(response) {
            //console.log(response.data);
            if(response.data.code == -1){
                layer.msg(response.data.msg,{time:2000});
                setTimeout(function () {
                    window.location.href = "/index.php/Home/agen/member"
                }, 1500)
            } else {
                successHandle(response, function () {
                    $scope.cart = response.data.data.prolist;
                    $scope.zong = response.data.data.prototal;
                    $scope.limitmoney = response.data.data.limitmoney;
                    $scope.level = response.data.data.level;
                    $scope.floor = function(number) {
                        return Math.floor(number);
                    };
                    //代理等级表
                    $scope.agenlevel = response.data.data.agenlevel;
                    $scope.selectlevel = response.data.data.level;
                });
            }
        }, function errorCallback(response) {
        });

        //总价计算
        $scope.jisuan = function (id) {
            $scope.zong = 0;
            for (var x in $scope.cart) {
                $scope.cart[x].num = Math.ceil($scope.cart[x].num);
                if($scope.cart[x].num <= 0 || $scope.cart[x].num > 99999){
                    $scope.cart[x].num = 0;
                    $scope.cart[x].prozong = 0;
                } else {
                    $scope.cart[x].prozong = $scope.cart[x].num * $scope.cart[x].myprice;
                    $scope.zong += $scope.cart[x].prozong;
                }
            }
            if($scope.zong >= $scope.limitmoney){
                $(".go-deal").removeClass("ungo-deal");
            } else if($scope.zong < $scope.limitmoney){
                $(".go-deal").addClass("ungo-deal");
            }

            /*var data = $scope.cart[id];
            $http({
                method:'POST',
                data:data,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: function(obj) {
                    var str = [];
                    for (var p in obj) {
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    }
                    return str.join("&");
                },
                url: "/index.php/Home/Login/changeNum"
            }).then(function successCallback(response) {
            }, function errorCallback(response) {
                errorHandle(response);
            });*/
        }

        //等级变更，重新获取代理价格和商品
        $scope.changelevel = function () {
            //$scope.selectlevel
            $http({
                method: 'POST',
                data: {'level':$scope.selectlevel},
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                transformRequest: function (obj) {
                    var str = [];
                    for (var p in obj) {
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    }
                    return str.join("&");
                },
                url: "/index.php/Home/Agen/changelevel"
            }).then(function successCallback(response) {
                //console.log(response.data.data.nearagen);
                if(response.data.code == -1){
                    layer.msg(response.data.msg,{time:2000});
                } else {
                    successHandle(response, function () {
                        $scope.cart = response.data.data.prolist;
                        $scope.zong = response.data.data.prototal;
                        $scope.limitmoney = response.data.data.limitmoney;
                    })
                }
            }, function errorCallback(response) {
                errorHandle(response);
            });
        }

        //提交报单
        $scope.sub = function () {
            layer.confirm('确认提交？', {
                btn: ['取消','确认'] //按钮
            }, function(){
                layer.msg('已取消');
            }, function() {
                var data = new Array();
                var i = 0;
                for (var c in $scope.cart) {
                    if ($scope.cart[c].num > 0) {
                        data[i] = $scope.cart[c].id;
                        data[i + 1] = $scope.cart[c].num;
                        i = i + 2;
                    }
                }
                if(i > 0) {
                    $http({
                        method: 'POST',
                        data: {'zong': $scope.zong, 'level': $scope.selectlevel, 'products':data},
                        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                        transformRequest: function (obj) {
                            var str = [];
                            for (var p in obj) {
                                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                            }
                            return str.join("&");
                        },
                        url: "/index.php/Home/AgentOnlineorder/upgradeagen"
                    }).then(function successCallback(response) {
                        //console.log(response.data);
                        if(response.data.code == -1){
                            layer.msg(response.data.msg,{time:2000});
                        } else {
                            layer.msg('提交成功！请等待审核！',{time:2000});
                            setTimeout(function () {
                                //successHandle(response, function () {
                                    window.location.href = "/index.php/Home/Agen/member"
                                //});
                            }, 2000)
                        }
                    }, function errorCallback(response) {
                        errorHandle(response);
                    });
                }
            })
        }
    })

</script>
</body>
</html>